"""
字符串 数据格式转换
"""

from my_typing import *


def camel_2_python(string: str) -> str:
    """
    驼峰体 -> 蛇形

        - createTime            ->      create_time
        - nextUserCode          ->      next_user_code
        - stringType            ->      string_type
        - valueLabelMapping     ->      value_label_mapping

    Args:
        string: 待转换的内容（驼峰体）

    Returns:
        转换后的内容（蛇形）

    Examples:
        >>> camel_2_python("createTime")
        "create_time"

        >>> camel_2_python("nextUserCode")
        "next_user_code"

        >>> camel_2_python("stringType")
        "string_type"
    """

    string_list: ListStr = []

    for idx, char in enumerate(string):
        if (
            char.isupper()
            and idx != 0
        ):
            string_list.append("_")

        string_list.append(char)

    return "".join(string_list).lower()


def python_2_camel(
    string: str,
    small_camel: bool = True,
) -> str:
    """
    蛇形 -> 驼峰体

        - create_time       ->      createTime
        - next_user_code    ->      nextUserCode
        - _next_user_code   ->      nextUserCode

    Args:
        string: 待转换的内容（蛇形）
        small_camel: 是否转换成小驼峰体（首字母小写）

    Returns:
        转换后的内容（驼峰体）

    Examples:
        >>> python_2_camel("create_time")
        "createTime"

        >>> python_2_camel("create_time", False)
        "CreateTime"
    """

    if not string:
        return ""

    res: str = "".join(
        i.capitalize()
        for i in filter(None, string.split("_"))
    ).replace("_", "")

    if small_camel:
        res = res[0].lower() + res[1::]

    return res
